package com.boxin.ims.modules.sys.scheduler;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.boxin.ims.modules.bi.dao.BIDao;

@Service
public class BiSceneProcessScheduler  extends AbstractScheduler {

	
	@Autowired
	 BIDao biDao;
	
	public BiSceneProcessScheduler() {
		super();
	}
	
	@Override
	public String frequency() {
		return this.getSchedulerInfo().getFrequence();
	}

	@Override
	public void process() {
		Calendar cal=Calendar.getInstance();
        cal.add(Calendar.DATE,-1);
        Date d=cal.getTime();


		 SimpleDateFormat sp=new SimpleDateFormat("yyyy-MM-dd");
		String createTime=sp.format(d);//获取昨天日期
		createTime=createTime.replace("-", "");
		 
		 Map<String, Object> params=new HashMap<String, Object>();
		params.put("day", createTime);
		
		//每天的message 拷贝
		biDao.copyWechatMessage(params);
		biDao.addBiAliveMember(params);
		biDao.updateBIAliveMember(params);
		
		 List<Map<String, Object>>  list=biDao.queryQrCodeSubscribeMessage(params);
		 
		 List<Map<String, Object>> SceneList= biDao.querySceneMessageByDay(params);
		 
		 for (int i = 0; i < SceneList.size(); i++) {
			 Map<String, Object> sceneParams=new HashMap<String, Object>();
			 sceneParams.put("day", createTime);
			 sceneParams.put("user_id", SceneList.get(i).get("user_id"));
			 String scene_id=SceneList.get(i).get("scene_id").toString();
			 Integer count=0;
			for (int j = 0; j < list.size(); j++) {
				if(list.get(j).get("user_id")==SceneList.get(i).get("user_id")&&sceneProcess(list.get(j).get("message").toString()).equals(scene_id)){
					count++;
				}
			}
			sceneParams.put("scene_id", scene_id);
			 sceneParams.put("count", count);
			biDao.updateBiScene(sceneParams);
		}
		
	}
	
	
	public String sceneProcess(String message){
		return message.substring(message.indexOf("qrscene_")+8, message.lastIndexOf("Ticket"));
	}

}
